<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('bill_exports', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('user_id');
            $table->unsignedBigInteger('book_id');
            $table->string('title'); // 导出标题
            $table->enum('export_range', ['all', 'month', 'custom']); // 导出范围
            $table->date('start_date')->nullable(); // 开始日期
            $table->date('end_date')->nullable(); // 结束日期
            $table->enum('format', ['excel', 'csv', 'pdf']); // 导出格式
            $table->enum('status', ['pending', 'processing', 'completed', 'failed', 'expired'])->default('pending'); // 状态
            $table->string('file_path')->nullable(); // 文件路径
            $table->timestamp('expires_at')->nullable(); // 过期时间
            $table->text('error_message')->nullable(); // 错误信息
            $table->timestamps();
            $table->softDeletes();

            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->foreign('book_id')->references('id')->on('money_books')->onDelete('cascade');

            $table->index(['user_id', 'created_at']);
            $table->index(['status', 'created_at']);
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('bill_exports');
    }
};
